<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="formRef" label-width="130px" :model="form" :rules="rules">
      <el-form-item label="业务类型" prop="BusinessClassName">
        <el-select v-model="form.BusinessClassID" disabled>
          <el-option
            :label="form.BusinessClassName"
            :value="form.BusinessClassID"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        v-if="form.ProductType == 0"
        label="签约产品名称"
        prop="ProductName"
      >
        <el-select v-model="form.ProductID" disabled>
          <el-option :label="form.ProductName" :value="form.ProductID" />
        </el-select>
      </el-form-item>
      <el-form-item v-else label="签约赠品名称" prop="ProductName">
        <el-select v-model="form.ProductID" disabled>
          <el-option :label="form.ProductName" :value="form.ProductID" />
        </el-select>
      </el-form-item>
      <el-form-item label="服务对象" prop="BindStoreID">
        <el-select
          v-model="form.BindStoreID"
          :disabled="disabled"
          @change="getStore(form.BindStoreID)"
        >
          <!-- <el-option label="无" :value="-1" /> -->
          <el-option :label="`新增${BindName}`" :value="0" />
          <el-option
            v-for="(item, index) in StoreOptions"
            :key="index"
            :label="item.StoreAccount"
            :value="item.StoreID"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        v-if="form.BindStoreID === 0"
        :label="`新增${BindName}`"
        prop="BindStoreAccount"
      >
        <el-input v-model="form.BindStoreAccount" :disabled="disabled" />
      </el-form-item>
      <el-form-item
        v-if="form.ProductID === 66"
        label="开发语言"
        prop="DevLanguage"
      >
        <el-input v-model="form.DevLanguage" :disabled="disabled" />
      </el-form-item>
      <el-form-item
        v-if="form.ProductID === 72"
        label="服务标准"
        prop="ServiceStandards"
      >
        <el-input
          v-model="form.ServiceStandards"
          type="textarea"
          :rows="2"
          :disabled="disabled"
        />
      </el-form-item>
      <el-form-item
        v-if="
          form.ProductID === 68 ||
          form.ProductID === 69 ||
          form.ProductID === 70
        "
        label="产品型号"
        prop="Version"
      >
        <el-input v-model="form.Version" :disabled="disabled" />
      </el-form-item>
      <el-form-item
        v-if="
          form.ProductID === 68 ||
          form.ProductID === 69 ||
          form.ProductID === 70 ||
          form.ProductID === 107
        "
        label="制作类型"
        prop="MakeType"
      >
        <!-- <el-input v-model="form.MakeType" /> -->
        <el-select v-model="form.MakeType">
          <el-option label="定制" value="定制" />
          <el-option label="模板" value="模板" />
          <el-option label="模板+修改" value="模板+修改" />
        </el-select>
      </el-form-item>
      <el-form-item
        v-if="
          form.ProductID === 68 ||
          form.ProductID === 69 ||
          form.ProductID === 70 ||
          form.ProductID === 107
        "
        label="模板号"
        prop="TemplateCode"
      >
        <el-input v-model="form.TemplateCode" />
      </el-form-item>
      <el-form-item
        v-if="
          form.ProductID === 68 ||
          form.ProductID === 69 ||
          form.ProductID === 70 ||
          form.ProductID === 107
        "
        label="语言版本"
        prop="LanguageVer"
      >
        <el-input v-model="form.LanguageVer" />
      </el-form-item>
      <el-form-item
        v-if="
          form.ProductID === 68 ||
          form.ProductID === 69 ||
          form.ProductID === 70 ||
          form.ProductID === 107
        "
        label="绑定域名"
        prop="BindDomains"
      >
        <el-input v-model="form.BindDomains" />
      </el-form-item>
      <el-form-item
        v-if="
          form.ProductID === 68 ||
          form.ProductID === 69 ||
          form.ProductID === 70 ||
          form.ProductID === 107
        "
        label="工单详情"
        prop="WorkOrderMemo"
      >
        <el-input
          v-model="form.WorkOrderMemo"
          type="textarea"
          :rows="2"
          :disabled="disabled"
        />
      </el-form-item>
      <el-form-item
        v-if="
          form.ProductID === 68 ||
          form.ProductID === 69 ||
          form.ProductID === 70 ||
          form.ProductID === 107
        "
        label="工单金额"
        prop="WorkOrderMoney"
      >
        <el-input v-model="form.WorkOrderMoney" :disabled="disabled" />
      </el-form-item>
      <el-form-item
        v-if="
          form.ProductID === 68 ||
          form.ProductID === 69 ||
          form.ProductID === 70 ||
          form.ProductID === 107
        "
        label="工单备注"
        prop="WebSiteMemo"
      >
        <el-input
          v-model="form.WebSiteMemo"
          type="textarea"
          :rows="2"
          :disabled="disabled"
        />
      </el-form-item>
      <!-- <el-form-item
        v-if="form.BindStoreID === 0 && form.BusinessClassID !== 5"
        label="密码"
        prop="BindStorePassword"
      >
        <el-input v-model="form.BindStorePassword" :disabled="disabled" />
      </el-form-item> -->
      <el-form-item
        v-if="form.BindStoreID === 0 && form.BusinessClassID !== 5"
        label="是否工厂店"
        prop="isFactory"
      >
        <el-select v-model="form.isFactory" :disabled="disabled">
          <el-option label="否" :value="0" />
          <el-option label="是" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="链接地址" prop="StoreUrl">
        <el-input v-model="form.StoreUrl" :disabled="disabled" />
      </el-form-item>
      <el-form-item label="备注" prop="StoreMemo">
        <el-input
          v-model="form.StoreMemo"
          type="textarea"
          :disabled="disabled"
        />
      </el-form-item>
      <el-form-item
        label="计划服务期限"
        prop="ServiceDates"
        style="width: 380px"
      >
        <DateSelect ref="DateSelectRef" @get-date="getDate" />
        <!-- <el-date-picker
          v-model="form.ServiceDates"
          type="daterange"
          unlink-panels
          start-placeholder="Start date"
          end-placeholder="End date"
          :default-time="defaultTime"
          :disabled="disabled"
        /> -->
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { StoreSelect } from '@/api/common'
  import { saveTrusteeship } from '@/api/businessOrder'
  import DateSelect from '/src/components/DateSelect.vue'
  export default defineComponent({
    name: 'Trusteeship',
    components: { DateSelect },
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')
      const { fetchData } = inject('rootMethods')
      const state = reactive({
        actstaffpostid: 0,
        disabled: false,
        formRef: null,
        initform: {},
        form: {},
        rules: {
          BindStoreID: [
            { required: true, trigger: 'blur', message: '请绑定店铺' },
          ],
          BindStoreAccount: [
            { required: true, trigger: 'blur', message: '请输入服务对象' },
          ],
          BindStorePassword: [
            { required: true, trigger: 'blur', message: '请输入密码' },
          ],
          isFactory: [
            { required: true, trigger: 'blur', message: '是否工厂店' },
          ],
          // StoreUrl: [
          //   { required: true, trigger: 'blur', message: '请输入链接' },
          // ],
          // ServiceDates: [
          //   { required: true, trigger: 'blur', message: '请选择服务期限' },
          // ],
        },
        DateSelectRef: null,
        title: '',
        dialogFormVisible: false,
        StoreOptions: [],
        BindName: '',
      })

      const showEdit = (row, OrderFlowState) => {
        const userInfo = JSON.parse(localStorage.getItem('userInfo'))
        const actstaffpost = JSON.parse(userInfo.StaffPostID)
        state.actstaffpostid = actstaffpost[0][1]

        console.log('product111', row)
        if (
          OrderFlowState >= 4 &&
          state.actstaffpostid != 10 &&
          state.actstaffpostid != 11 &&
          state.actstaffpostid != 42
        ) {
          state.disabled = true
        }
        state.title = row.ProductName
        state.form = Object.assign({}, row)
        state.initform = Object.assign({}, row)
        state.form.ServiceDates = [
          row.PlanServiceStartDate,
          row.PlanServiceEndDate,
        ]

        nextTick(() => {
          state.DateSelectRef.edtiTime(
            row.PlanServiceStartDate,
            row.PlanServiceEndDate
          )
        })

        if (state.form.BusinessClassID === 5) {
          state.BindName = state.form.ProductName
        } else {
          state.BindName = '店铺账号'
        }
        console.log('state.form0000', state.form)
        getData()
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {}
        state.dialogFormVisible = false
      }
      const save = () => {
        console.log('save.form', state.form)
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await saveTrusteeship(state.form)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            fetchData()
            state.DateSelectRef.empty()
            close()
          }
        })
      }

      const getStore = (StoreID) => {
        console.log('StoreID', StoreID)
        if (StoreID > 0) {
          const expForm = state.StoreOptions.find(
            (item) => item.StoreID === StoreID
          )
          console.log('expForm', state.form, expForm)

          if (expForm) {
            state.form = Object.assign(state.form, expForm.info)
            state.form.StoreUrl = expForm.StoreUrl
          }
        } else {
          state.form = Object.assign({}, state.initform)
          state.form.BindStoreID = StoreID
        }
      }

      const getData = async () => {
        state.listLoading = true

        const $params = {
          CustomerID: state.form.CustomerID,
          OrderID: state.form.OrderID,
          OrderListID: state.form.OrderListID,
          ProductID: state.form.ProductID,
        }
        const {
          data: { StoreOptions },
        } = await StoreSelect($params)
        state.StoreOptions = StoreOptions
        if (StoreOptions.length > 0) {
          const expForm = state.StoreOptions.find(
            (item) => item.StoreID === state.form.BindStoreID
          )

          console.log('expForm', state.form, expForm)

          if (expForm) {
            const expFormInfo = expForm.info
            delete expFormInfo.ProductID
            delete expFormInfo.ProductName
            state.form = Object.assign({}, state.form, expFormInfo)
            console.log('state.form', state.form)
          }
        }

        state.listLoading = false
      }

      const getDate = (data) => {
        state.form.ServiceDates = data
        console.log(data, '子组件传过来的时间数组')
      }
      return {
        ...toRefs(state),
        showEdit,
        getData,
        getDate,
        close,
        save,
        getStore,
      }
    },
  })
</script>
